import javax.management.RuntimeErrorException;
import java.io.*;
import java.nio.file.Files;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Scanner;

public class IODemo {
    public static void main(String[] args) {
        System.out.println("请输入要查找的目录:");
        Scanner scanner = new Scanner(System.in);
        String directory = scanner.next();
        System.out.println("请输入要查找的文件内容:");
        String content = scanner.next();
        File dire = new File(directory);
        if(!dire.isDirectory()){
            System.out.println("输入目录错误!");
            return;
        }
        scanFile(dire , content);
    }
    private static void scanFile(File dire , String content){
        File[] files = dire.listFiles();
        if(files == null){
            return ;
        }
        for (File file:
             files) {
            if(file.isFile()){
                sreachStr(file , content);
            }else if(file.isDirectory()){
                scanFile(file , content);
            }else{
                ;
                int a = 0;
            }
        }
        List<List<Integer>> list = new ArrayList<>();
    }
    private static void sreachStr(File file , String content) {
        try (InputStream inputStream = new FileInputStream(file)){
            StringBuilder stringBuilder = new StringBuilder();
            while(true){
                byte[] arr = new byte[1024];
                int n = inputStream.read(arr);
                if(n == -1){
                    break;
                }
                String s = new String(arr , 0 , n);
                stringBuilder.append(s);
            }
            if(stringBuilder.indexOf(content) == -1){
            }else{
                String s= file.getAbsolutePath();
                System.out.println("找到文件" + file.getName() +s);
            }
        }catch (IOException e){
            throw new RuntimeException(e);
        }
    }

}
